package com.young.common.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 配置参数容器
 * @author wangjiyu@imdada.cn
 * @create 2018/10/9
 */
public class PropertyHolder extends PropertyPlaceholderConfigurer {

    //参数配置
    private static Map<String, String> MAP = new HashMap<String, String>();

    /**
     * 获取配置值
     * @param key 配置key
     * @return
     */
    public static String getProperty(String key){
        return MAP.get(key);
    }

    /**
     * 处理配置文件中的参数
     * @param beanFactoryToProcess
     * @param props
     * @throws BeansException
     */
    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
        //调用父类的默认处理
        super.processProperties(beanFactoryToProcess, props);

        //个性化处理
        if (props != null && !props.isEmpty()){
            for(Object key : props.keySet()){
                String k = String.valueOf(key);
                String val = props.getProperty(k);
                MAP.put(k, val);
                //System.out.println("PropertiesHelper 配置项："+k+" = "+val);
            }
        }

    }
}
